package com.woniuxy.coachservice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.coachclient.client.VenueFeignClient;
import com.woniuxy.coachclient.client.dto.VenueLoginDto;
import com.woniuxy.coachdao.mapper.CoachInfoMapper;
import com.woniuxy.coachdao.mapper.JoinVenueMapper;
import com.woniuxy.coachdao.model.CoachInfo;
import com.woniuxy.coachdao.model.JoinVenue;
import com.woniuxy.coachmodel.dto.JoinVenueDto;
import com.woniuxy.coachmodel.form.CoachDisposeForm;
import com.woniuxy.coachservice.exception.CoachException;
import com.woniuxy.coachservice.service.JoinVenueService;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.status.CoachState;
import com.woniuxy.core.status.VenueState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 入驻商家表 服务实现类
 * </p>
 *
 * @author 冉杰
 * @since 2024-01-15
 */
@Service
public class JoinVenueServiceImpl extends ServiceImpl<JoinVenueMapper, JoinVenue> implements JoinVenueService {
    @Autowired
    private JoinVenueMapper joinVenueMapper;
    @Autowired
    private CoachInfoMapper coachInfoMapper;
    @Resource
    private VenueFeignClient venueFeignClient;
    @Override
    public void joinVenue(Integer venueId, Integer coachId, String message) {
        QueryWrapper<JoinVenue> wrapper = new QueryWrapper<>();
        wrapper.eq("coach_id",coachId);
        wrapper.eq("venue_id",venueId);
        wrapper.eq("type",1);
        wrapper.ne("do_state",0);
        JoinVenue joinVenue1 = joinVenueMapper.selectOne(wrapper);
        if(!ObjectUtil.isEmpty(joinVenue1)){
            throw new CoachException(10011,"该场馆已申请，请勿重复申请");
        }
        CoachInfo coachInfo = coachInfoMapper.selectById(coachId);
        if(coachInfo.getCoachState()!= CoachState.IS_ENABLE.getState()){
            throw new CoachException(10001,"账号状态异常");
        }
        if(coachInfo.getAuthState()!=CoachState.IS_AUTH.getState()){
            throw new CoachException(10002,"账号未认证");
        }
        if(!ObjectUtil.isEmpty(coachInfo.getVenueId())){
            throw new CoachException(10003,"已入驻场馆");
        }
        Result<VenueLoginDto> result = venueFeignClient.byId(venueId);
        if(result.getCode()!=200){
            throw new CoachException(result.getCode(),result.getMsg());
        }
        VenueLoginDto data = result.getData();
        if(data.getAuthState()!= VenueState.IS_AUTH.getState()){
            throw new CoachException(10004,"场馆未认证");
        }
        if(data.getVenueState()!=VenueState.IS_ENABLE.getState()){
            throw new CoachException(10005,"场馆账号状态异常");
        }
        JoinVenue joinVenue = new JoinVenue();
        joinVenue.setVenueId(venueId);
        joinVenue.setVenueName(data.getVenueName());
        joinVenue.setCoachId(coachId);
        joinVenue.setCoachName(coachInfo.getCoachName());
        joinVenue.setMessage(message);
        joinVenue.setDoState(2);//申请中的状态
        joinVenue.setType(1);//申请场馆的状态
        joinVenueMapper.insert(joinVenue);
    }

    @Override
    public void inviteCoach(Integer venueId, Integer coachId, String message) {
        QueryWrapper<JoinVenue> wrapper = new QueryWrapper<>();
        wrapper.eq("coach_id",coachId);
        wrapper.eq("venue_id",venueId);
        wrapper.eq("type",0);
        wrapper.ne("do_state",0);
        JoinVenue joinVenue1 = joinVenueMapper.selectOne(wrapper);
        if(!ObjectUtil.isEmpty(joinVenue1)){
            throw new CoachException(10012,"该教练已邀请，请勿重复邀请");
        }
        Result<VenueLoginDto> result = venueFeignClient.byId(venueId);
        if(result.getCode()!=200){
            throw new CoachException(result.getCode(),result.getMsg());
        }
        VenueLoginDto data = result.getData();
        if(data.getAuthState()!= VenueState.IS_AUTH.getState()){
            throw new CoachException(10004,"场馆未认证");
        }
        if(data.getVenueState()!=VenueState.IS_ENABLE.getState()){
            throw new CoachException(10005,"场馆账号状态异常");
        }
        CoachInfo coachInfo = coachInfoMapper.selectById(coachId);
        if(coachInfo.getCoachState()!= CoachState.IS_ENABLE.getState()){
            throw new CoachException(10001,"账号状态异常");
        }
        if(coachInfo.getAuthState()!=CoachState.IS_AUTH.getState()){
            throw new CoachException(10002,"账号未认证");
        }
        if(!ObjectUtil.isEmpty(coachInfo.getVenueId())){
            throw new CoachException(10003,"已入驻场馆");
        }
        JoinVenue joinVenue = new JoinVenue();
        joinVenue.setVenueId(venueId);
        joinVenue.setVenueName(data.getVenueName());
        joinVenue.setCoachId(coachId);
        joinVenue.setCoachName(coachInfo.getCoachName());
        joinVenue.setMessage(message);
        joinVenue.setDoState(2);//申请中的状态
        joinVenue.setType(0);//被场馆邀请的状态
        joinVenueMapper.insert(joinVenue);
    }

    @Override
    public List<JoinVenueDto> coachLook(Integer coachId) {
        QueryWrapper<JoinVenue> wrapper = new QueryWrapper<>();
        wrapper.eq("coach_id",coachId);
        List<JoinVenue> list = joinVenueMapper.selectList(wrapper);
        List<JoinVenueDto> dtos = BeanUtil.copyToList(list, JoinVenueDto.class);
        return dtos;
    }

    @Override
    public List<JoinVenueDto> venueLook(Integer venueId) {
        QueryWrapper<JoinVenue> wrapper = new QueryWrapper<>();
        wrapper.eq("venue_id",venueId);
        List<JoinVenue> list = joinVenueMapper.selectList(wrapper);
        List<JoinVenueDto> dtos = BeanUtil.copyToList(list, JoinVenueDto.class);
        return dtos;
    }

    @Override
    public JoinVenue look(Integer joinId) {
        JoinVenue joinVenue = joinVenueMapper.selectById(joinId);
        return joinVenue;
    }

    @Override
    public void coachAgree(CoachDisposeForm form, Integer coachId) {
        JoinVenue joinVenue = joinVenueMapper.selectById(form.getJoinId());
        if(ObjectUtil.isEmpty(joinVenue)){
            throw new CoachException(20001,"申请记录不存在");
        }
        if(joinVenue.getCoachId()!=coachId){
            throw new CoachException(20002,"教练信息异常");
        }
        if(joinVenue.getType()!=0){
            throw new CoachException(20003,"邀请类型状态异常");
        }
        if(joinVenue.getDoState()!=2){
            throw new CoachException(20004,"处理状态异常");
        }
        joinVenue.setDoState(1);//将处理状态更改为通过
        joinVenue.setMessage(form.getMessage());
        joinVenueMapper.updateById(joinVenue);
        //更新教练信息中的场馆id
        CoachInfo coachInfo = coachInfoMapper.selectById(joinVenue.getCoachId());
        coachInfo.setVenueId(joinVenue.getVenueId());
        coachInfoMapper.updateById(coachInfo);
    }

    @Override
    public void coachRefuse(CoachDisposeForm form, Integer coachId) {
        JoinVenue joinVenue = joinVenueMapper.selectById(form.getJoinId());
        if(ObjectUtil.isEmpty(joinVenue)){
            throw new CoachException(20001,"申请记录不存在");
        }
        if(joinVenue.getCoachId()!=coachId){
            throw new CoachException(20002,"教练信息异常");
        }
        if(joinVenue.getType()!=0){
            throw new CoachException(20003,"邀请类型状态异常");
        }
        if(joinVenue.getDoState()!=2){
            throw new CoachException(20004,"处理状态异常");
        }
        joinVenue.setDoState(0);//将处理状态更改为拒绝
        joinVenue.setMessage(form.getMessage());
        joinVenueMapper.updateById(joinVenue);
    }

    @Override
    public void venueAgree(CoachDisposeForm form, Integer venueId) {
        JoinVenue joinVenue = joinVenueMapper.selectById(form.getJoinId());
        if(ObjectUtil.isEmpty(joinVenue)){
            throw new CoachException(20001,"申请记录不存在");
        }
        if(joinVenue.getVenueId()!=venueId){
            throw new CoachException(20005,"场馆信息异常");
        }
        if(joinVenue.getType()!=1){
            throw new CoachException(20003,"邀请类型状态异常");
        }
        if(joinVenue.getDoState()!=2){
            throw new CoachException(20004,"处理状态异常");
        }
        joinVenue.setDoState(1);
        joinVenue.setMessage(form.getMessage());
        joinVenueMapper.updateById(joinVenue);
        //更新教练信息中的场馆id
        CoachInfo coachInfo = coachInfoMapper.selectById(joinVenue.getCoachId());
        coachInfo.setVenueId(joinVenue.getVenueId());
        coachInfoMapper.updateById(coachInfo);
    }

    @Override
    public void venueRefuse(CoachDisposeForm form, Integer venueId) {
        JoinVenue joinVenue = joinVenueMapper.selectById(form.getJoinId());
        if(ObjectUtil.isEmpty(joinVenue)){
            throw new CoachException(20001,"申请记录不存在");
        }
        if(joinVenue.getVenueId()!=venueId){
            throw new CoachException(20005,"场馆信息异常");
        }
        if(joinVenue.getType()!=1){
            throw new CoachException(20003,"邀请类型状态异常");
        }
        if(joinVenue.getDoState()!=2){
            throw new CoachException(20004,"处理状态异常");
        }
        joinVenue.setDoState(0);
        joinVenue.setMessage(form.getMessage());
        joinVenueMapper.updateById(joinVenue);
    }


}
